<template>
  <div class="goods-comment">
    <div class="head" v-if="commentData">
      <div class="data">
        <p>
          <span>{{ commentData.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{commentData.praisePercent}}</span
          ><span>好评率</span>
        </p>
        <p>
          <span>{{ commentData.evaluateCount }}</span
          ><span>条评价</span>
        </p>
        <p>
          <span>{{ commentData.hasPictureCount }}</span>
          <span>条有图评价</span>
        </p>
      </div>
    </div>

    <!-- 排序 -->
    <div class="sort">
      <span>排序：</span>
      <a
        href="javascript:;"
        :class="{active: reqParams.sortField === null}"
        @click="changeSortField(null)"
        >默认</a
      >
      <a
        href="javascript:;"
        :class="{active: reqParams.sortField === 'praiseCount'}"
         @click="changeSortField('praiseCount')"
        >最热</a
      >
      <a
        href="javascript:;"
        :class="{active: reqParams.sortField === 'createTime'}"
         @click="changeSortField('createTime')"
        >最新</a
      >
      <XtxCheckbox v-model="reqParams.hasPicture" style="margin-left:50px;">是否有图</XtxCheckbox>
    </div>

    <!-- 评论列表 -->
    <div class="list">
      <div class="item" v-for="item in commentList" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="">
          <span>{{ item.member.nickname }}</span>
        </div>
        <div class="body">
          <div class="score">
            <!-- 评分星星 -->
            <!-- 满星是5颗星 score当前得了几颗星 -->
            <!-- 实心星星 -->
            <i
              v-for="i in item.score"
              :key="i + '1'"
              class="iconfont icon-wjx01"
            ></i>
            <!-- 空心星星 -->
            <i
              v-for="i in 5 - item.score"
              :key="i + '2'"
              class="iconfont icon-wjx02"
            ></i>
            <span class="attr">颜色：黑色 尺码：M</span>
          </div>
          <div class="text">{{ item.content }}</div>
          <!-- 图片预览组件 -->
          <GoodsCommentImage :pictures="item.pictures" v-if="item.pictures.length > 0" />
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 加载更多评论 -->
    <XtxInfiniteLoad :isLoading="isLoad" :isFinished="isFinish" @load="loadCom" />

  </div>
</template>
<script>
import { findCommentInfoByGoods, findCommentListByGoods } from '@/api/goods'
import { ref, onMounted, reactive, watch } from 'vue'
import { useRoute } from 'vue-router'
import GoodsCommentImage from './goods-comment-image'
export default {
  name: 'GoodsComment',
  components: {
    GoodsCommentImage
  },
  setup () {
    const route = useRoute()
    // 获取评价数据
    const commentData = ref({})
    async function loadCommentData () {
      const res = await findCommentInfoByGoods(route.params.id)
      // console.log('评价数据', res)
      commentData.value = res.result
      isLoad.value = false
    }
    onMounted(() => {
      loadCommentData()
    })

    // 使用基础字段获取基础列表
    // 筛选条件准备
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: false, // 是否有图
      sortField: null // 排序: praiseCount -> 最热 createTime -> 最新
    })

    const commentList = ref({}) // 评论列表
    async function loadCommentList () {
      const res = await findCommentListByGoods(route.params.id, reqParams)
      console.log('评论列表', res)
      commentList.value = res.result.items
    }

    onMounted(() => {
      loadCommentList()
    })

    // 排序字段的修改
    function changeSortField (type) {
      reqParams.sortField = type
      // 1. 激活的交互效果(添加class类名)
      // 2. 使用修改完的排序字段重新获取评论列表
      loadCommentList()
    }

    // 是否有图
    // 一旦绑定的数据发生变化 就重新发送请求
    watch(
      () => {
        return reqParams.hasPicture
      },
      () => {
        loadCommentList() // 重新发送请求
      }
    )

    // 加载更多评论
    const isLoad = ref(true)
    const isFinish = ref(false)
    async function loadCom () {
      isLoad.value = true // true 当前在获取数据中，通知不可以再重复获取数据
      reqParams.page++
      const res = await findCommentListByGoods(route.params.id, reqParams)
      console.log('下一页数据', res)
      // 是否还有数据
      if (res.result.page === '9') {
        isFinish.value = true // 停止监听
      }
      commentList.value = [...commentList.value, ...res.result.items] // 拼接数据
      isLoad.value = false // 数据获取完毕，isLoad设置为fasle,通知可以获取下一页数据
    }
    return { commentData, commentList, changeSortField, reqParams, loadCom, isLoad, isFinish }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      flex:1;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
}
.list {
  padding: 0 20px;
  .item {
    display: flex;
    padding: 25px 10px;
    border-bottom: 1px solid #f5f5f5;
    .user {
      width: 160px;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        overflow: hidden;
      }
      span {
        padding-left: 10px;
        color: #666;
      }
    }
    .body {
      flex: 1;
      .score {
        line-height: 40px;
        .iconfont {
          color: #ff9240;
          padding-right: 3px;
        }
        .attr {
          padding-left: 10px;
          color: #666;
        }
      }
    }
    .text {
      color: #666;
      line-height: 24px;
    }
    .time {
      color: #999;
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
    }
  }
}

</style>
